package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.PxAdversterSlotService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.PxAdversterSlot;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cwj
 * @description
 * @date 2025/4/11 10:22
 **/
@Controller
@RequestMapping("pxAdversterSlot")
public class PxAdversterSlotController extends BaseController {

    @Resource
    private PxAdversterSlotService pxAdversterSlotService;

    /***
     * @description 查询广告位
     * @author cwj
     * @param [page]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/11 11:27
     **/
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<PxAdversterSlot> pages = pxAdversterSlotService.list(page);
        mv.addObject("adversterSlots", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("adversters/px_adverster_slot_list");
        return mv;
    }


    /**
     * @description 添加广告位
     * @author cwj
     * @param []
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/11 11:53
     **/
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("adversters/px_adverster_slot_add");
        return mv;
    }


    /**
     * @description 增加广告位
     * @author cwj
     * @param [pxAdversterSlot]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/11 15:15
     **/
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(PxAdversterSlot pxAdversterSlot) {
        ModelAndView mv = new ModelAndView();
        pxAdversterSlotService.add(pxAdversterSlot,this.getSessionUser(),this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @description 判断是否已经存在同名的广告位
     * @author cwj
     * @param [id, name]
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/4/11 15:40
     **/
    @ResponseBody
    @RequestMapping("checkName")
    public Response<Object> checkName(@RequestParam(required = false) String id, String name,String code) {
        List<PxAdversterSlot> pxAdversterSlots = pxAdversterSlotService.getByNameOrCode(name,code);
        if (CollectionUtils.isEmpty(pxAdversterSlots)) {
            return Response.successResponse();
        }
        if (StringUtil.isBlank(id)) {
            return Response.failedResponse("相同名称或编码的广告位已存在，请修改");
        } else {
            boolean hasConflict = pxAdversterSlots.stream().anyMatch(slot -> !slot.getId().equals(id));
            if (hasConflict) {
                return Response.failedResponse("相同名称或编码的广告位已存在，请修改");
            }
        }
        return Response.successResponse();
    }

    /**
     * @description 显示广告为编辑页面
     * @author cwj
     * @param [id]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/11 17:32
     **/
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("adversterSlot", pxAdversterSlotService.findById(new ObjectId(id)));
        mv.setViewName("adversters/px_adverster_slot_edit");
        return mv;
    }

    /**
     * @description 编辑广告位
     * @author cwj
     * @param [pxAdversterSlot]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/11 17:35
     **/
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(PxAdversterSlot pxAdversterSlot) {
        ModelAndView mv = new ModelAndView();
        if(ObjectUtils.isEmpty(pxAdversterSlot) || StringUtils.isEmpty(pxAdversterSlot.getId())){
            mv.addObject("msg", "fail");
            mv.setViewName("save_result");
            return mv;
        }
        pxAdversterSlotService.update(pxAdversterSlot, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

}
